<?php
/**
 * store后台， hm，wax测肤分数关联店铺自己商品
 * Author: lyun
 * Date: 2019/4/1
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Store;

use App\Http\Controllers\Admin\Customer\HmSkinSuggestController;
use App\Http\Controllers\Admin\Customer\WaxSkinSuggestController;
use App\Http\Controllers\ApiController;
use App\Http\Controllers\Service\Customer\RecommendProductService;
use App\Http\Requests\Admin\Store\RecommendProduct\RecommendProductDeleteRequest;
use App\Http\Requests\Admin\Store\RecommendProduct\RecommendProductSingleStoreRequest;
use App\Http\Requests\Admin\Store\RecommendProduct\RecommendProductUpdateRequest;
use App\Models\Admin\StoreAdmin;
use App\Models\Customer\CustomerRelateHmSkin;
use App\Models\Customer\CustomerRelateSkinProductable;
use App\Models\Customer\CustomerRelateWaxSkin;
use App\Models\Customer\Language;
use App\Models\Panx\Category;
use App\Models\Panx\Product;
use App\Models\Store\Store;
use App\Services\Presenters\HmSkinData;
use App\Services\Presenters\WaxSkinData;
use App\Services\Utils\Guard;

class RecommendProductController extends ApiController
{
    const ROUTES = [
        'hmIndex'     => 'api.admin.store.recommend.product.hm.index',
        'waxIndex'    => 'api.admin.store.recommend.product.wax.index',
        'create'      => 'api.admin.store.recommend.product.create',
        'singleStore' => 'api.admin.store.recommend.product.single.store',
        'update'      => 'api.admin.store.recommend.product.update',
        'destroy'     => 'api.admin.store.recommend.product.destroy',
    ];

    /**
     * customer后台，返回客户hm测肤分数和推荐商品的关系列表
     * @test
     *
     * @return array|\Illuminate\Http\Response
     */
    public function hmIndex()
    {
        /** @var Store $store */
        $store = Guard::store();
        $store_id = $store->id;
        $customer_id = $store->customer_id;

        $recommend_product_service = (new RecommendProductService);

        $hm_skin_relate_recommend_products = $recommend_product_service->getSkinRelateRecommendProducts($customer_id, class_basename(CustomerRelateHmSkin::class), $store_id);

        return $this->response(compact('hm_skin_relate_recommend_products'));
    }

    /**
     * customer后台，返回客户wax测肤分数和推荐商品的关系列表
     * @test
     *
     * @return array|\Illuminate\Http\Response
     */
    public function waxIndex()
    {
        /** @var Store $store */
        $store = Guard::store();
        $store_id = $store->id;
        $customer_id = $store->customer_id;

        $recommend_product_service = (new RecommendProductService);

        $wax_skin_relate_recommend_products = $recommend_product_service->getSkinRelateRecommendProducts($customer_id, class_basename(CustomerRelateWaxSkin::class), $store_id);

        return $this->response(compact('wax_skin_relate_recommend_products'));
    }

    /**
     * customer后台，返回客户商品列表，hm和wax的测肤建议列表
     *
     * @return array|\Illuminate\Http\Response
     */
    public function create()
    {
        /** @var Store $store */
        $store = Guard::store();
        $store_id = $store->id;
        $customer_id = $store->customer_id;
        $language_id = Language::getLanguageIds()[app()->getLocale()];

        $products = Product::filterOnShelf()
            ->whereIn('category_id', Category::hasAssetIds(Category::NO_ASSET))
            ->with('thumbImage')
            ->where('customer_id', $customer_id)
            ->where('store_id', $store_id)
            ->get(['id', 'name', 'title']);
        (new RecommendProductService)->getProductThumbImage($products);

        // 不关联测肤建议，只是默认的customer_id = 0;
        $suggest_customer_id = CustomerRelateHmSkin::CUSTOMER_ID_DEFAULT;

        $hm_skin_suggests = (new HmSkinSuggestController)->getHmSkinSuggests($suggest_customer_id, $language_id);
        $wax_skin_suggests = (new WaxSkinSuggestController)->getWaxSkinSuggests($suggest_customer_id, $language_id);

        $hm_skin_suggests = $hm_skin_suggests->first()['suggest_by_language'] ?? null;
        $wax_skin_suggests = $wax_skin_suggests->first()['suggest_by_language'] ?? null;

        // 检测项的分数等级信息
        $wax_level_sort_map = WaxSkinData::WAX_DETECTION_LEVEL_MAP;
        $hm_level_sort_map = HmSkinData::HM_DETECTION_LEVEL_MAP;

        return $this->response(compact(
            'products', 'hm_skin_suggests', 'wax_skin_suggests',
            'wax_level_sort_map', 'hm_level_sort_map'));
    }

    /**
     * customer后台，新增单个hm/wax测肤选项关联商品
     * @test
     *
     * @param RecommendProductSingleStoreRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function singleStore(RecommendProductSingleStoreRequest $request)
    {
        // 超管才能操作的
        $this->checkIsTrue(StoreAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        /** @var Store $store */
        $store = Guard::store();
        $store_id = $store->id;
        $customer_id = $store->customer_id;

        $products = $request->products;

        if ($request->skin_type === class_basename(CustomerRelateWaxSkin::class)) {

            $wax_skin = $request->wax_skin;
            $stored = (new RecommendProductService)->storeWaxCustomerSkinItemLevel($products, $wax_skin, $customer_id, $store_id);
        } else {

            $hm_skin = $request->hm_skin;
            $stored = (new RecommendProductService)->storeHmCustomerSkinItemLevel($products, $hm_skin, $customer_id, $store_id);
        }

        return $this->response(compact('stored'));
    }

    /**
     * customer后台，更新hm/wax测肤选项关联商品
     * @test
     *
     * @param RecommendProductUpdateRequest $request
     * @param $skin_id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(RecommendProductUpdateRequest $request, $skin_id)
    {
        // 超管才能操作的
        $this->checkIsTrue(StoreAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        /** @var Store $store */
        $store = Guard::store();
        $store_id = $store->id;
        $customer_id = $store->customer_id;

        $product_ids = $request->product_ids;
        $skin_type = $request->skin_type;

        if ($skin_type === class_basename(CustomerRelateWaxSkin::class)) {
            $customer_relate_skins = CustomerRelateWaxSkin::getCustomerRelateWaxSkinByIds($customer_id, $store_id, [$skin_id]);
        } else {
            $customer_relate_skins = CustomerRelateHmSkin::getCustomerRelateHmSkinByIds($customer_id, $store_id, [$skin_id]);
        }

        $customer_relate_skin = $customer_relate_skins->first();
            $this->checkItemExist($customer_relate_skin);

        $updated = (new RecommendProductService)->updateCustomerRelateSkinProduct($customer_relate_skin, $product_ids);

        return $this->response(compact('updated'));
    }

    /**
     * customer后台，删除hm/wax测肤选项关联商品
     * @test
     *
     * @param RecommendProductDeleteRequest $request
     * @param $customer_id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy(RecommendProductDeleteRequest $request, $customer_id)
    {
        // 超管才能操作的
        $this->checkIsTrue(StoreAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        /** @var Store $store */
        $store = Guard::store();
        $store_id = $store->id;
        $customer_id = $store->customer_id;
        $skin_type = $request->skin_type;
        $skin_ids = $request->skin_ids;

        if ($skin_type === class_basename(CustomerRelateWaxSkin::class)) {
            $customer_relate_skin = CustomerRelateWaxSkin::getCustomerRelateWaxSkinByIds($customer_id, $store_id, $skin_ids);
        } else {
            $customer_relate_skin = CustomerRelateHmSkin::getCustomerRelateHmSkinByIds($customer_id, $store_id, $skin_ids);
        }

        $skin_ids = $customer_relate_skin->pluck('id')->all();

        $deleted = (new CustomerRelateSkinProductable)->whereIn('productable_id', $skin_ids)
            ->where('productable_type', $skin_type)
            ->delete();

        return $this->response(compact('deleted'));
    }
}
